from spass.core.error import error
from spass.core.builtins import (is_builtin, run_builtin)
from spass.core.plugins import (plugin_init, is_plugin, run_plugin)

import spass.core.db.env
import os

def is_inited(argv):
    if os.getenv('SPASS_ROOT') == None:
        error('not.root', argv)
        return False

    if (not argv[1] == 'init' and not argv[1] == 'help' and 
        not argv[1] == 'version') and (
        not os.path.exists(os.getenv('SPASS_ROOT')+
                           "/core/.initialized")):
        error('init.find', ['', ''])
        return False
    return True

def parse_commande(argv):
    if is_builtin(argv[1]):
        return run_builtin(argv[1], argv[2:])
    elif plugin_init() and is_plugin(argv[1]):
        return run_plugin(argv[1], argv[2:])
    else:
        error('command.find', argv[1:])
        return 1
